package com.entmanage.base.collegeco.service.impl;

import java.util.List;

import com.entmanage.base.collegeco.domain.*;
import com.entmanage.base.collegeco.mapper.CollegecoInternMapper;
import com.entmanage.common.utils.DateUtils;
import com.entmanage.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import com.entmanage.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.entmanage.base.collegeco.service.ICollegecoInternService;

/**
 * 实习生信息Service业务层处理
 *
 * @author lsq
 * @date 2025-07-31
 */
@Service
public class CollegecoInternServiceImpl implements ICollegecoInternService
{
    @Autowired
    private CollegecoInternMapper hrmInternMapper;

    /**
     * 查询实习生信息
     *
     * @param internId 实习生信息主键
     * @return 实习生信息
     */
    @Override
    public CollegecoIntern selectHrmInternByInternId(Long internId)
    {
        return hrmInternMapper.selectHrmInternByInternId(internId);
    }

    /**
     * 查询实习生信息列表
     *
     * @param hrmIntern 实习生信息
     * @return 实习生信息
     */
    @Override
    public List<CollegecoIntern> selectHrmInternList(CollegecoIntern hrmIntern)
    {
        return hrmInternMapper.selectHrmInternList(hrmIntern);
    }

    /**
     * 新增实习生信息
     *
     * @param hrmIntern 实习生信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHrmIntern(CollegecoIntern hrmIntern)
    {
        // 保存用户和部门id
        hrmIntern.setUserId(SecurityUtils.getUserId());
        hrmIntern.setDeptId(SecurityUtils.getDeptId());
        hrmIntern.setCreateTime(DateUtils.getNowDate());
        int rows = hrmInternMapper.insertHrmIntern(hrmIntern);
        insertHrmInternTask(hrmIntern);
        insertHrmInternEvaluation(hrmIntern);
        insertHrmInternReport(hrmIntern);
        insertHrmInternWeeklyLog(hrmIntern);
        return rows;
    }

    /**
     * 修改实习生信息
     *
     * @param hrmIntern 实习生信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHrmIntern(CollegecoIntern hrmIntern)
    {
        hrmIntern.setUpdateTime(DateUtils.getNowDate());
        hrmInternMapper.deleteHrmInternTaskByInternId(hrmIntern.getInternId());
        insertHrmInternTask(hrmIntern);
        hrmInternMapper.deleteHrmInternEvaluationByInternId(hrmIntern.getInternId());
        insertHrmInternEvaluation(hrmIntern);
        hrmInternMapper.deleteHrmInternReportByInternId(hrmIntern.getInternId());
        insertHrmInternReport(hrmIntern);
        hrmInternMapper.deleteHrmInternWeeklyLogByInternId(hrmIntern.getInternId());
        insertHrmInternWeeklyLog(hrmIntern);
        return hrmInternMapper.updateHrmIntern(hrmIntern);
    }

    /**
     * 批量删除实习生信息
     *
     * @param internIds 需要删除的实习生信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrmInternByInternIds(Long[] internIds)
    {
        hrmInternMapper.deleteHrmInternTaskByInternIds(internIds);
        hrmInternMapper.deleteHrmInternEvaluationByInternIds(internIds);
        hrmInternMapper.deleteHrmInternReportByInternIds(internIds);
        hrmInternMapper.deleteHrmInternWeeklyLogByInternIds(internIds);
        return hrmInternMapper.deleteHrmInternByInternIds(internIds);
    }

    /**
     * 删除实习生信息信息
     *
     * @param internId 实习生信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrmInternByInternId(Long internId)
    {
        hrmInternMapper.deleteHrmInternTaskByInternId(internId);
        hrmInternMapper.deleteHrmInternEvaluationByInternId(internId);
        hrmInternMapper.deleteHrmInternReportByInternId(internId);
        hrmInternMapper.deleteHrmInternWeeklyLogByInternId(internId);
        return hrmInternMapper.deleteHrmInternByInternId(internId);
    }

    /**
     * 新增实习生任务书信息
     *
     * @param hrmIntern 实习生信息对象
     */
    public void insertHrmInternTask(CollegecoIntern hrmIntern)
    {
        List<CollegecoInternTask> hrmInternTaskList = hrmIntern.getHrmInternTaskList();
        Long internId = hrmIntern.getInternId();
        if (StringUtils.isNotNull(hrmInternTaskList))
        {
            List<CollegecoInternTask> list = new ArrayList<CollegecoInternTask>();
            for (CollegecoInternTask hrmInternTask : hrmInternTaskList)
            {
                hrmInternTask.setInternId(internId);
                list.add(hrmInternTask);
            }
            if (list.size() > 0)
            {
                hrmInternMapper.batchHrmInternTask(list);
            }
        }
    }

    /**
     * 新增实习生成绩鉴定主信息
     *
     * @param hrmIntern 实习生信息对象
     */
    public void insertHrmInternEvaluation(CollegecoIntern hrmIntern)
    {
        List<CollegecoInternEvaluation> hrmInternEvaluationList = hrmIntern.getHrmInternEvaluationList();
        Long internId = hrmIntern.getInternId();
        if (StringUtils.isNotNull(hrmInternEvaluationList))
        {
            List<CollegecoInternEvaluation> list = new ArrayList<CollegecoInternEvaluation>();
            for (CollegecoInternEvaluation hrmInternEvaluation : hrmInternEvaluationList)
            {
                hrmInternEvaluation.setInternId(internId);
                list.add(hrmInternEvaluation);
            }
            if (list.size() > 0)
            {
                hrmInternMapper.batchHrmInternEvaluation(list);
            }
        }
    }

    /**
     * 新增实习报告信息
     *
     * @param hrmIntern 实习生信息对象
     */
    public void insertHrmInternReport(CollegecoIntern hrmIntern)
    {
        List<CollegecoInternReport> hrmInternReportList = hrmIntern.getHrmInternReportList();
        Long internId = hrmIntern.getInternId();
        if (StringUtils.isNotNull(hrmInternReportList))
        {
            List<CollegecoInternReport> list = new ArrayList<CollegecoInternReport>();
            for (CollegecoInternReport hrmInternReport : hrmInternReportList)
            {
                hrmInternReport.setInternId(internId);
                list.add(hrmInternReport);
            }
            if (list.size() > 0)
            {
                hrmInternMapper.batchHrmInternReport(list);
            }
        }
    }

    /**
     * 新增实习周报（每周提交一次）信息
     *
     * @param hrmIntern 实习生信息对象
     */
    public void insertHrmInternWeeklyLog(CollegecoIntern hrmIntern)
    {
        List<CollegecoInternWeeklyLog> hrmInternWeeklyLogList = hrmIntern.getHrmInternWeeklyLogList();
        Long internId = hrmIntern.getInternId();
        if (StringUtils.isNotNull(hrmInternWeeklyLogList))
        {
            List<CollegecoInternWeeklyLog> list = new ArrayList<CollegecoInternWeeklyLog>();
            for (CollegecoInternWeeklyLog hrmInternWeeklyLog : hrmInternWeeklyLogList)
            {
                hrmInternWeeklyLog.setInternId(internId);
                list.add(hrmInternWeeklyLog);
            }
            if (list.size() > 0)
            {
                hrmInternMapper.batchHrmInternWeeklyLog(list);
            }
        }
    }
}
